package GUI;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;
import Server.EchoServer;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 *@author Juan Alapin - 324507516
 *@author Giora Yaffe - 036551281
 *@author Shachar Gritzman - 300174026
 *@author Nir Porat - 021515580
*/

public class serverGUI extends JFrame implements ActionListener, KeyListener{
	
	private static final long serialVersionUID = 1L;
	public static Component mainframe;
	public JPasswordField pwdBraude;
	public JTextField txtRoot;
	public JButton btnStart;
	public JButton btnExit;
	public JButton btnLogout;
	private EchoServer es;
	private JTextField textField_1;
	private JTextField txtLocalhost;
	private JTextField textField_3;
	private JTextField txtIcm;

	
	public serverGUI() {
		super();
		setBounds(100, 100, 340, 420);
		this.setTitle("SQL server connection");
		getContentPane().setLayout(null);
		
		
		JLabel lblSqlUsername = new JLabel("SQL username:");
		lblSqlUsername.setBounds(10, 186, 110, 21);
		getContentPane().add(lblSqlUsername);
		
		JLabel lblSqlPassword = new JLabel("SQL password:");
		lblSqlPassword.setBounds(10, 218, 110, 21);
		getContentPane().add(lblSqlPassword);
		
		pwdBraude = new JPasswordField("braude");
		pwdBraude.setBounds(200, 218, 100, 20);
		getContentPane().add(pwdBraude);
		
		txtRoot = new JTextField("root");
		txtRoot.setBounds(200, 186, 100, 20);
		getContentPane().add(txtRoot);
		txtRoot.setColumns(10);
		
		btnStart = new JButton("Start");
		btnStart.setBounds(10, 336, 89, 23);
		btnStart.addActionListener(this);
		getContentPane().add(btnStart);
		
		JLabel lblServerPort = new JLabel("server port");
		lblServerPort.setBounds(10, 281, 82, 21);
		getContentPane().add(lblServerPort);
		
		textField_1 = new JTextField();
		textField_1.setText("5555");
		textField_1.setBounds(200, 281, 100, 20);
		getContentPane().add(textField_1);
		textField_1.setColumns(10);
		
		JLabel lblSqlServerIp = new JLabel("SQL server IP:");
		lblSqlServerIp.setBounds(10, 49, 82, 21);
		getContentPane().add(lblSqlServerIp);
		
		JLabel lblSqlServerPort = new JLabel("SQL server port:");
		lblSqlServerPort.setBounds(10, 81, 110, 21);
		getContentPane().add(lblSqlServerPort);
		
		JLabel lblSqlServerDb = new JLabel("SQL server DB name:");
		lblSqlServerDb.setBounds(10, 113, 143, 21);
		getContentPane().add(lblSqlServerDb);
		
		txtLocalhost = new JTextField("localhost");
		txtLocalhost.setColumns(10);
		txtLocalhost.setBounds(200, 49, 100, 20);
		getContentPane().add(txtLocalhost);
		
		textField_3 = new JTextField("3306");
		textField_3.setColumns(10);
		textField_3.setBounds(200, 81, 100, 20);
		getContentPane().add(textField_3);
		
		txtIcm = new JTextField("ICM");
		txtIcm.setColumns(10);
		txtIcm.setBounds(200, 113, 100, 20);
		getContentPane().add(txtIcm);
		
		btnExit = new JButton("Exit");
		btnExit.setBounds(211, 336, 89, 23);
		btnExit.addActionListener(this);
		getContentPane().add(btnExit);
		
		btnLogout = new JButton("Logout");
		btnLogout.setEnabled(false);
		btnLogout.setBounds(109, 336, 89, 23);
		btnLogout.addActionListener(this);
		getContentPane().add(btnLogout);
		this.setVisible(true);
	}



	@Override
	public void actionPerformed(ActionEvent e) {
		
		if (e.getSource() == btnStart) {
			es = new EchoServer(Integer.parseInt(textField_1.getText()),txtLocalhost.getText(), Integer.parseInt(textField_3.getText()),txtRoot.getText(), txtIcm.getText(), new String(pwdBraude.getPassword()));
		
	
		try {
			es.listen(); // Start listening for connections
			btnStart.setEnabled(false);
			txtIcm.setEnabled(false);
			txtLocalhost.setEnabled(false);
			textField_3.setEnabled(false);
			textField_1.setEnabled(false);
			txtRoot.setEnabled(false);
			pwdBraude.setEnabled(false);
			btnLogout.setEnabled(true);
		} catch (Exception ex) {
			JOptionPane.showMessageDialog(serverGUI.mainframe,"ERROR - Could not listen for clients!");

		}
	}
		if (e.getSource() == btnLogout) {
		
			btnStart.setEnabled(true);
			txtIcm.setEnabled(true);
			txtLocalhost.setEnabled(true);
			textField_3.setEnabled(true);
			textField_1.setEnabled(true);
			txtRoot.setEnabled(true);
			pwdBraude.setEnabled(true);
			btnLogout.setEnabled(false);
			if (es.isListening()) {
				try {
					es.close();
				} catch (Exception ex) {
					JOptionPane.showMessageDialog(serverGUI.mainframe,
							"ERROR - Could not close connection.");

				}
			}
		}
		if (e.getSource() == btnExit) {
			btnLogout.doClick();
			System.exit(0);
		}
	}
	
	public static void main(String[] args) {

		serverGUI turnOnSrv = new serverGUI();
	}



	@Override
	public void keyPressed(KeyEvent e) {
		int key = e.getKeyCode();
		if(key == KeyEvent.VK_ENTER){
			if(e.getSource() == txtIcm || e.getSource() == txtLocalhost || e.getSource() == textField_3 || e.getSource() == textField_1 || e.getSource() == txtRoot || e.getSource() == pwdBraude )
				btnStart.doClick();
		}
		if (key == KeyEvent.VK_ESCAPE)
			btnExit.doClick();
	}



	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}	
	
	
}

